#pragma once
#include <sstream>
#include <memory>
#include "NetProtocol.h"

class CHttpClient : public INetProtocol
{
    struct BlockData {    
        int64_t offset; 
        int64_t total; 
        int64_t readSize = 0; 
        std::stringstream ss;
        BlockData(int64_t off, int64_t sz):offset(off), total(sz){
        }

        void WriteData(void *data, size_t sz)
        {
            ss.write((const char*)data, sz);
            readSize += sz;
        }
    };
public:
    ~CHttpClient();
    int64_t GetFileSize(const std::string& url)override;
    bool Download(const std::string& url,  int64_t offset, int64_t blocksize )override;
    std::iostream& GetStream()override;

    std::string GetContextMd5(const std::string &url);

    static size_t WriteBlock(void *data, size_t size, size_t nmemb, void *clientp);
    static size_t ReadHeader(void *data, size_t size, size_t nmemb, void *param);
private:
    std::shared_ptr<BlockData> m_blockData;
};